<?php

namespace app\common\dao\user;

use app\common\model\user\User as model;
use app\common\dao\BaseDao;
use app\common\repositories\user\UserOperationRepository;
use think\db\BaseQuery;

class UserDao extends BaseDao
{
    /**
    * 设置模型
    * @return model
    */
    protected function getModel(): string
    {
        return model::class;
    }


    /**
     * @param $where
     * @return \think\db\BaseQuery
     * User: xiest
     * Date: 2021/10/6
     */
    public function search($where)
    {
        $query = ($this->getModel()::getDB());

        $searchWhere = [];
//        if(isset($where['business_id']) && $where['business_id']!=''){
//            array_push($searchWhere,'business_id');
//        }
//        if(isset($where['identity_id']) && $where['identity_id']!=''){
//            array_push($searchWhere,'identity_id');
//        }
//
//        if(isset($where['city_id']) && $where['city_id']!=''){
//            array_push($searchWhere,'city_id');
//        }
//        $query->withSearch($searchWhere,$where);

        $query->when(isset($where['keyword']) && $where['keyword'] !== '',function (BaseQuery $query)use($where){
            $query->where('nickname','like',"%{$where['keyword']}%");
        })->when(isset($where['status']) && $where['status'] !== '',function (BaseQuery $query)use($where){
            $query->where('status',$where['status']);
        })->when(isset($where['phone']) && $where['phone'] !== '',function (BaseQuery $query)use($where){
            $query->where('phone','like',"%{$where['phone']}%");
        })->when(isset($where['sign_time']) && $where['sign_time'] !== '',function (BaseQuery $query)use($where){
            getModelTime($query,$where['sign_time'],'sign_time');
        })->when(isset($where['create_time']) && $where['create_time'] !== '',function (BaseQuery $query)use($where){
            getModelTime($query,$where['create_time']);
        });

        return $query;
    }





    public function userInfoByOpenId($openid)
    {
        return ($this->getModel())::getDB()->where('openid',$openid)->find();
    }

    public function userInfoByWechatOpenId($openid)
    {
        return ($this->getModel())::getDB()->where('wechat_openid',$openid)->find();
    }

    public function userInfoByAppOpenId($openid)
    {
        return ($this->getModel())::getDB()->where('app_openid',$openid)->find();
    }

    public function userInfoByUnionId($openid)
    {
        return ($this->getModel())::getDB()->where('unionid',$openid)->find();
    }

    public function userInfoByShareCode($shareCode)
    {
        return ($this->getModel())::getDB()->where('share_code',$shareCode)->find();
    }

    public function userInfoByPhone($phone)
    {
        return ($this->getModel())::getDB()->where('phone',$phone)->find();
    }


    public function getLock($id,$field='*',$lock = true)
    {
        return $this->getModel()::getInstance()->lock($lock)->field($field)->where($this->getPK(),$id)->find();
    }

}